package com.binguo.wxapi.controller;

import com.binguo.wxapi.form.WXGongZHMessageFrom;
import com.binguo.wxapi.form.WXGongZHSubMessageForm;
import com.binguo.wxapi.form.WxGonZHImgForm;
import com.binguo.wxapi.result.Result;
import com.binguo.wxapi.result.ResultGenerator;
import com.binguo.wxapi.service.WXGongZHService;
import io.swagger.annotations.Api;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.io.IOException;
import java.util.Map;

import static org.springframework.http.MediaType.APPLICATION_JSON_VALUE;

/**
 * @author yuritian
 * @Title: WXGongZHController
 * @ProjectName binguoDevelop
 * @Description: TODO
 * @date 2019-12-1713:29
 */
@Api(tags = "公众号权限")
@RestController
@RequestMapping
public class WXGongZHController {

    private static final Logger logger = LoggerFactory.getLogger(WXGongZHController.class);

    @Autowired
    WXGongZHService wxGongZHService;




    @PostMapping(value = "/api/wxapp/uploadface",produces = APPLICATION_JSON_VALUE)
    public Result uploadFace(@RequestBody @Valid WxGonZHImgForm form){
        wxGongZHService.uploadFace(form.getMediaId(),form.getCustomerId());
        return ResultGenerator.genSuccessResult();
    }

    @GetMapping(value = "/api/wxapp/getwxgzhConfig", produces = APPLICATION_JSON_VALUE)
    public Result getwxgzhConfig(@RequestParam String url) {
        return ResultGenerator.genSuccessResult(wxGongZHService.getGongZHTokenVO(url));
    }

    /**
     * 发送一次性订阅消息
     * @param wxGongZHSubMessageForm
     * @return
     * @throws IOException
     */
    @PostMapping(value = "/api/wxapp/getCustomerConfirm",produces = APPLICATION_JSON_VALUE)
    public Result getCustomerConfirm(@RequestBody WXGongZHSubMessageForm wxGongZHSubMessageForm)throws IOException {
        if(wxGongZHSubMessageForm.getAction() != "confirm"){
            return null;
        }else{
            logger.info(wxGongZHSubMessageForm.toString());
            Map<String,Object> map = wxGongZHService.sendSubMessage(wxGongZHSubMessageForm);
            if((Integer)map.get("errcode") != 0){
                return ResultGenerator.genFailResult(map.get("errmsg").toString());
            }else{
                return ResultGenerator.genSuccessResult(map);
            }
        }
    }

    /**
     * 发送公众号模版消息对外接口(如果内部想要调用直接调wxGongZHService中的sengMessage()方法)
     * @param wXGongZHMessageFrom
     * @return
     * @throws IOException
     */
    @PostMapping(value = "/api/wxapp/sendGZHMessage",produces = APPLICATION_JSON_VALUE)
    public Result sendGZHMessage(@RequestBody WXGongZHMessageFrom wXGongZHMessageFrom)throws IOException {
        if(wXGongZHMessageFrom.getTouser() == null ||
            wXGongZHMessageFrom.getData() == null ||
            wXGongZHMessageFrom.getTouser() == null ||
            wXGongZHMessageFrom.getTemplate_id() == null ){
            return  ResultGenerator.genFailResult("参数错误");
        }else{
            logger.info(wXGongZHMessageFrom.toString());
            Map<String,Object> map = wxGongZHService.sendMessage(wXGongZHMessageFrom);
            if((Integer)map.get("errcode") != 0){
                return ResultGenerator.genFailResult(map.get("errmsg").toString());
            }else{
                return ResultGenerator.genSuccessResult(map);
            }
        }
    }
}
